package cn.tedu.demo.service.impl;

import cn.tedu.demo.entity.Book;
import cn.tedu.demo.entity.User;
import cn.tedu.demo.mapper.BookMapper;
import cn.tedu.demo.service.BookService;
import cn.tedu.demo.vo.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public void save(Book book) {
        bookMapper.insert(book);
    }

    @Override
    public void update(Book book) {
        bookMapper.updateById(book);
    }

    @Override
    public Book checkBook(Integer id) {
        return null;
    }

    @Override
    public PageInfo getByPage(PageInfo pageInfo) {
        IPage<Book> page=new Page<>(pageInfo.getPageNum(),pageInfo.getPageSize());
        boolean flag= StringUtils.hasLength(pageInfo.getQuery());
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag, "name",pageInfo.getQuery());
        page=bookMapper.selectPage(page, queryWrapper);
        pageInfo.setTotal(page.getTotal());
        pageInfo.setRows(page.getRecords());
        return pageInfo;
    }

    @Override
    public void delete(Long id) {
        bookMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Integer> ids) {
        bookMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Book> getByUserid(Integer id) {
        List<Book> bookList=bookMapper.getByUserid(id);
        return bookList;
    }
}
